﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace mlnx_os_sysdump_analyzer
{
    class EthInfo
    {
        ArrayList items = new ArrayList();
        private bool KEEP_RUNNING; 
        private string OUTPUT_DIR_PATH;
        //
        //----------------------------------------------------
        //
        public EthInfo(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
        }
        //
        //----------------------------------------------------
        //
        public ArrayList getEthInfo()
        {
            return items;
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "ethinfo.txt");
            if (path != null)
            {
                scanEthInfo(path);
            }
        }
        //
        //----------------------------------------------------
        //
        public void scanEthInfo(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                Item tmpItem = new Item();
                int counter = 0;
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    try
                    {
                        // read line
                        string line = sr.ReadLine();

                        if (line.Contains("========="))
                        {
                            if (tmpItem.itemName.Equals(""))
                            {
                                if (counter == 0)
                                {
                                    counter++;
                                }
                            }
                            else
                            {
                                counter++;
                                if (counter == 4)
                                {
                                    counter = 1;
                                    items.Add(tmpItem);
                                    tmpItem = new Item();
                                }
                            }
                        }
                        else
                        {
                            if (counter == 1 && !line.Equals(""))
                            {
                                if (tmpItem.itemName.Equals(""))
                                {
                                    tmpItem = new Item();
                                    tmpItem.itemName = line;
                                    tmpItem.content = line + "\r\n\r\n";
                                    counter++;
                                }
                            }
                            else
                                if (counter == 3)
                                {
                                    tmpItem.content += line + "\r\n";
                                }
                        }
                    }
                    catch
                    {
                    }
                }
                // add the last one
                if (items != null && tmpItem != null)
                {
                    items.Add(tmpItem);
                }
            }
            catch
            {  }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //
    }
}
